<?php

class CourseForm extends BaseForm {
  const FORM_NAME           = 'frm';

  const FIELD_TITLE         = 'title';
  const FIELD_DESCRIPTION   = 'description';
  const FIELD_CATEGORY      = 'category';
  const FIELD_STATUS        = 'status';

  public function configure() {
    $arrStatusList    = StringUtil::getStatusArray();
    $arrCategoryList  = CATEGORYTable::getInstance()->getCategoryArray();

    $widget = array(
            self::FIELD_TITLE       => new sfWidgetFormInputText(array(), array('maxlength' => 255)),
            self::FIELD_DESCRIPTION => new sfWidgetFormTextareaTinyMCE(array(), array()),
            self::FIELD_CATEGORY      => new sfWidgetFormSelect(array('choices' => $arrCategoryList), array('multiple' => false, 'expand' => true)),
            self::FIELD_STATUS      => new sfWidgetFormSelect(array('choices' => $arrStatusList), array('multiple' => false, 'expand' => true))
    );

    $labels = array(
            self::FIELD_TITLE       => 'Title<req />',
            self::FIELD_DESCRIPTION => 'Description<req />',
            self::FIELD_CATEGORY    => 'Category<req />',
            self::FIELD_STATUS      => 'Status<req />'
    );

    $this->setWidgets($widget);
    $this->widgetSchema->setLabels($labels);

    $validators = array(
            self::FIELD_TITLE       => new sfValidatorString(array('trim' => true), array('required' => LangUtil::getText('error_required', array($labels[self::FIELD_TITLE])))),
            self::FIELD_DESCRIPTION => new sfValidatorString(array('trim' => true), array('required' => LangUtil::getText('error_required', array($labels[self::FIELD_DESCRIPTION])))),
            self::FIELD_STATUS      => new sfValidatorString(array('trim' => true), array('required' => LangUtil::getText('error_required', array($labels[self::FIELD_STATUS]))))
    );

    $this->setValidators($validators);
    //$this->validatorSchema->setPostValidator(new sfValidatorCallback(array('callback' => array($this, 'postValidator'))));

    $this->widgetSchema->setNameFormat(self::FORM_NAME . '[%s]');

    $this->validatorSchema->setOption('allow_extra_fields', true);
    $this->validatorSchema->setOption('filter_extra_fields', false);
  }

  public function postValidator($validator, $values) {
    return $values;
  }

  public function populateData(COURSE $course) {
    $this->setDefault(self::FIELD_TITLE, $course->getTitle());
    $this->setDefault(self::FIELD_DESCRIPTION, $course->getDescription());
    $this->setDefault(self::FIELD_CATEGORY, $course->getCategoryId());
    $this->setDefault(self::FIELD_STATUS, $course->getIsActive());
  }
}
?>